package com.rui.utils;

import org.springframework.core.convert.converter.Converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

//将字符串转为Date
public class CustomDateConverter implements Converter<String, Date> {

    @Override
    public Date convert(String s) {
        Date date = null;
        try {
            if (s.length() == 8) {
                date = new SimpleDateFormat("HH:mm:ss").parse(s);
            } else if (s.length() == 10) {
                date = new SimpleDateFormat("yyyy-MM-dd").parse(s);
            } else if (s.length() == 19) {
                date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(s);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
}
